package me.softwareengineer.guice.rendering.domain;

import java.util.List;
import java.util.Map;

import me.softwareengineer.guice.rendering.listener.EndGroupEvent;
import me.softwareengineer.guice.rendering.listener.Listener;
import me.softwareengineer.guice.rendering.listener.StartGroupEvent;
import me.softwareengineer.guice.rendering.util.CollectionUtil;

/**
 * Represents a grouping of elements
 * 
 * @author yadtahir
 * 
 *         <pre>
 * This class is thread-safe and can be cached/re-used and used concurrently.
 * </pre>
 */
public class GroupElement extends Element {

	/**
	 * Creates a Grouping of elements without children. This is useful when the
	 * user wants adds ({@link #addChild(Element)}) children manually after the
	 * element is created.
	 */
	public GroupElement() {
		this(CollectionUtil.getEmptyChildren());
	}

	/**
	 * Creates a group element with children
	 * 
	 * @param children
	 *            the children elements of the element.
	 */
	public GroupElement(List<Element> children) {
		super(children);
	}

	/**
	 * Creates a group element with children and properties
	 * 
	 * @param children
	 *            the children elements of the element.
	 * @param properties
	 *            the properties of the element. For more info, see
	 *            {@link HasProperties}
	 */
	public GroupElement(List<Element> children, Map<String, String> properties) {
		super(children, properties);
	}

	/**
	 * Creates a group element with children and properties
	 * 
	 * @param properties
	 *            the properties of the element. For more info, see
	 *            {@link HasProperties}
	 */
	public GroupElement(Map<String, String> properties) {
		super(properties);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void afterElement(Listener listener) {
		EndGroupEvent e = new EndGroupEvent(getAllProperties());
		listener.onEvent(e);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void beforeElement(Listener listener) {
		StartGroupEvent e = new StartGroupEvent(getAllProperties());
		listener.onEvent(e);
	}
}
